package cn.com.dhc.zzz.back.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.format.ZZZFormat;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.util.CommonUntil;
import cn.bbs.ba.util.DtoUtil;
import cn.com.zzz.dao.PkAccInfoDao;
import cn.com.zzz.dao.PkJFFlowDao;
import cn.com.zzz.pk.dto.PkAccInfo;
import cn.com.zzz.pk.dto.PkJF;
import cn.com.zzz.pk.dto.PkJFFlow;

/**
 * 添加用户
 */
public class PKAccInfoAction extends BAAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 判断是否登录
		super.execute(mapping, form, request, response);
		if(super.userInfo == null) {
			return mapping.findForward("comError");
		}
		PkAccInfo accInfo = new PkAccInfo();
		DtoUtil.formToDto(form, accInfo);
		String strUpdateFlag = accInfo.getUpdateFlag();
		if (StringUtils.isEmpty(strUpdateFlag)) {
			return mapping.findForward("comError");
		}
		PkAccInfoDao userInfoDao = (PkAccInfoDao)BABeanFactory.getBean("ACCINFO");
		int insertCnt = 0;
		// 添加账号
		if ("addAccInfo".equals(strUpdateFlag)) {
			// 密码
			accInfo.setPwd(BAMima.encry(super.userInfo.getAlias() + accInfo.getPwd()));
			accInfo.setUserNo(super.userInfo.getUserNo());
			// 插入数据库
			insertCnt = userInfoDao.insertJF(accInfo);
			if (insertCnt == 1) {
				// 建立登陆SESSION
				request.setAttribute("showValue", ZPaiConstant.disPlay("添加成功，请牢记次账户取款密码"));
				//先到手机验证页面
				//return mapping.findForward("userCenter-13");
				// 会到主页 手机验证取消
				return mapping.findForward("success");
			}
			else {
				request.setAttribute("showValue", ZPaiConstant.disPlay("添加失败，请确认是否已经添加过一次"));
				// 用户已经存在
				return mapping.findForward("success");
			}
		}
		// 管理账号--删除。
		else if("deleteAccInfo".equals(strUpdateFlag)) {
			accInfo.setUserNo(super.userInfo.getUserNo());
			insertCnt = userInfoDao.delete(accInfo);
			
			if (insertCnt == 1) {
				// 建立登陆SESSION
				request.setAttribute("showValue", ZPaiConstant.disPlay("删除成功"));
				//先到手机验证页面
				//return mapping.findForward("userCenter-13");
				// 会到主页 手机验证取消
				return mapping.findForward("success");
			}
			else {
				request.setAttribute("showValue", ZPaiConstant.disPlay("删除失败"));
				// 用户已经存在
				return mapping.findForward("success");
			}
		}
		// 取款
		else if ("getMoneyByAccInfo".equals(strUpdateFlag)) {
			HttpSession  session = request.getSession();
			// 验证码验证
			String captcha = CommonUntil.objToString(session.getAttribute("captcha"));
			if (!captcha.equals(request.getParameter("checkCode"))) {
				request.setAttribute("showValue", ZPaiConstant.disPlay("验证码不对，请重新输入。"));
				// 用户已经存在
				return mapping.findForward("success");
			}
			accInfo.setPwd(BAMima.encry(super.userInfo.getAlias() + accInfo.getPwd()));
			accInfo.setUserNo(super.userInfo.getUserNo());
			List<PkAccInfo> list = userInfoDao.getAccInfo(accInfo, "0");
			if (list.size() == 0) {
				request.setAttribute("showValue", ZPaiConstant.disPlay("账号密码不正确，请重新输入。"));
				// 用户已经存在
				return mapping.findForward("success");
			}
			// 如果要取的钱不够。
			if (CommonUntil.compare(((PkJF)super.userInfo.getValue("JF")).getDqfs(), request.getParameter("Amt")) < 0) {
				request.setAttribute("showValue", ZPaiConstant.disPlay("你没有这么多钱了。"));
				// 用户已经存在
				return mapping.findForward("success");
			}
			PkJFFlow jfFlow = new PkJFFlow();
			
			jfFlow.setUserNo(super.userInfo.getUserNo());
			// 房间号码
			jfFlow.setFjNo("0000000000");
			// 流水号
			jfFlow.setFlowNo(ZZZFormat.getSysteTime());
			// 消费积分
			jfFlow.setXfjf(request.getParameter("Amt"));
			// 桌子号码
			jfFlow.setZzNo("0000000000");
			// 值1 0：处理中，1  成功 2，申请取款，3 已经打款，4 返回取款申请
			jfFlow.setEx1("2");
			jfFlow.setRemarks(ZPaiConstant.disPlay("主动申请取款"));
			jfFlow.setExDate(ZZZFormat.getTime());
			jfFlow.setPkVa(accInfo.getAccNo());
			
			PkJFFlowDao pkJFFlowDao = (PkJFFlowDao)BABeanFactory.getBean("JFFLOW");
			insertCnt = pkJFFlowDao.insertJF(jfFlow);
			
			if (insertCnt == 1) {
				// 建立登陆SESSION
				request.setAttribute("showValue", ZPaiConstant.disPlay("申请取款成功，大于在1-2天到账，请及时查询。"));
				//先到手机验证页面
				//return mapping.findForward("userCenter-13");
				// 会到主页 手机验证取消
				return mapping.findForward("success");
			}
			else {
				request.setAttribute("showValue", ZPaiConstant.disPlay("申请失败，有问题请联系管理员！"));
				// 用户已经存在
				return mapping.findForward("success");
			}
		}
		// 取金额流水
		else if ("getAccInfoFlow".equals(strUpdateFlag)){
			PkJFFlowDao accInfoDao = (PkJFFlowDao)BABeanFactory.getBean("JFFLOW");
			PkJFFlow accInfo1 = new PkJFFlow();
			
			accInfo1.setUserNo(super.userInfo.getUserNo());
			accInfo1.setValue("beginDate", ZZZFormat.getDifferentDate(-180, "yyyyMMddHHmmss"));
			accInfo1.setValue("endDate", ZZZFormat.getTime());
			PrintWriter writer = response.getWriter();
			JSONArray curArr = JSONArray.fromObject(accInfoDao.getJF(accInfo1, request.getParameter("rowNum")));
			writer.print(curArr.toString());
			writer.flush();
			writer.close();
			return mapping.findForward("ajaxCommon");
		}
		request.setAttribute("showValue", ZPaiConstant.disPlay("你要做的业务尚未完成，请稍等"));
		// 用户已经存在
		return mapping.findForward("success");
	}

}
